<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { reqFindListForUser, reqPreApplicationRun } from '@/pages/member/api/flowapp.ts'
import { MsgError, MsgSuccess } from '@/utils/utils.ts'
import { useRouter } from 'vue-router'
const router = useRouter() // 路由

const keyword = ref('')
const searchType = ref('')

const total = ref(0)
const pageNo = ref(1)

watch([keyword, pageNo], () => appPageList())

onMounted(() => appPageList())

const flowList = ref([])

const appPageList = async () => {
  const reqData: queryAppParams = {
    pageNo: pageNo.value,
    title: keyword.value,
  }
  let resp = await reqFindListForUser(reqData)
  if (resp.code === 0) {
    flowList.value = resp.data.list
    total.value = resp.data.total
  }
}
const activeNames = 1
const toDetail = async (item: any, flowId: number) => {
  const resp = await reqPreApplicationRun(item.applicationId)
  if (resp.code === 0) {
    // MsgSuccess('环境创建成功')
    router.push({
      path: '/smart/detail',
      query: {
        flowId: flowId,
        appId: item.applicationId,
        runId: resp.data.runId,
        applicationId: resp.data.applicationId,
        smartFlowId: resp.data.smartFlowId,
        title: item.title,
        handlerType: 'read',
      },
    })
  } else {
    // MsgError('环境创建失败')
  }
}
</script>

<template>
  <div class="smart-flow-container">
    <div class="common-banner">
      <img src="@/assets/images/square/app-market-banner.png" alt="" />
      <div class="content">
        <div class="common-w-box">
          <div class="txt-c">
            <div class="t1">智能流程</div>
            <div class="search-c">
              <el-input
                v-model="keyword"
                placeholder="请输入需要搜索的内容"
                class="input-with-select"
                size="large"
              >
                <template #prepend>
                  <el-select
                    v-model="searchType"
                    placeholder="Select"
                    style="width: 130px"
                    size="large"
                  >
                    <el-option label="全部" value="" />
                    <el-option label="常规应用" value="1" />
                    <el-option label="多组学" value="2" />
                    <el-option label="单细胞组学" value="3" />
                    <el-option label="空间组学" value="4" />
                  </el-select>
                </template>
                <template #append>
                  <el-button type="primary" link class="search-btn">
                    搜索
                  </el-button>
                </template>
              </el-input>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="common-w-box">
      <div class="cnb">
        <div class="common-slider" style="display: none">
          <div class="title">全部分类</div>
          <div class="menu-box">
            <ul>
              <li class="on">
                所有流程
                <el-icon class="el-icon--right">
                  <ArrowRight />
                </el-icon>
              </li>
              <li>多组学</li>
              <li>单细胞组学</li>
              <li>空间组学</li>
            </ul>
          </div>
        </div>
        <div class="cn-list">
          <div class="title">所有流程</div>
          <el-collapse accordion v-model="activeNames">
            <el-collapse-item
              v-for="(item, index) in flowList"
              :name="index + 1"
              :key="index + 1"
            >
              <template #title>
                <div class="bt-c">
                  {{ item.flowName }}
                </div>
                <div>
                  <span class="f1">ID：{{ item.flowNameEn }}</span>
                  <el-icon class="header-icon">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      :content="item.description"
                      placement="top"
                    >
                      <info-filled />
                    </el-tooltip>
                  </el-icon>
                </div>
              </template>
              <el-row :gutter="24" class="common-lists" v-for="(app, app_index) in item.applications">
                <el-col :span="8">
                  <div class="item" @click="toDetail(app, item.flowId)">
                    <div class="img">
                      <img :src="app.image" alt="" />
                    </div>
                    <div class="txt-c">
                      <div class="name">{{ app.title }}</div>
                      <div class="tag">{{ app.titleEn }}</div>
                      <div class="des">
                        {{ app.description }}
                      </div>
                    </div>
                  </div>
                </el-col>
              </el-row>
            </el-collapse-item>
          </el-collapse>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.common-lists .item .img {
  overflow: hidden;
  img {
    width: 100%;
  }
}
.smart-flow-container {
  .cnb {
    display: flex;
    padding: 16px 0;

    :deep(.el-collapse) {
      border-top: none;
      border-bottom: none;

      .el-collapse-item {
        margin-bottom: 20px;
        border-bottom: none;
      }

      .el-collapse-item__header {
        justify-content: space-between;
        padding: 0 4px 0 16px;
        background: #f5f8fa;
        border-bottom: none;
        border-radius: 4px;
      }

      .el-collapse-item__content {
        padding: 0 16px;
        background: #f5f8fa;
      }

      .el-collapse-item__wrap {
        border-bottom: none;
      }
    }

    .cn-list {
      flex: 1;
      padding: 20px 24px;
      margin-left: 16px;
      background: #fff;
      border-radius: 4px;

      .bt-c {
        flex: 1;
        text-align: left;
      }

      .f1 {
        color: #9ba1a8;
      }

      .title {
        margin-bottom: 20px;
        font-size: 16px;
        font-weight: 500;
      }
    }
  }
}
</style>
